package com.android.kmlmrg.utils;



import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.android.kmlmrg.model.User;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;



public class MethodeUtils {

	
	public static String getTimestamp () {

		
		return "" + System.currentTimeMillis();
	}
	
	
	public static String getHash (String pWsCallerSecret, String pTimeStamp ) {
		try {
			String vHash =Sha1Utils.sha(pWsCallerSecret+pTimeStamp.toString());
			return vHash;
		} catch (NoSuchAlgorithmException e) {			
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<User> getListUserFromJsonList ( String pJson ) {
		  
		ArrayList<User> list = new ArrayList<User>();
		pJson = pJson.replace("{\"userDto\":", "");
		pJson = pJson.substring(0, pJson.length()-1);
		
		JsonParser parser = new JsonParser();
		  JsonArray Jarray = parser.parse(pJson).getAsJsonArray();
		  Gson gson = new Gson();
		  
		  for(JsonElement obj : Jarray )
		    {  
			  
			  User cse = gson.fromJson( obj , User.class);
			  list.add(cse);
		    }
		  return list;
	}
	
	public static User getUserFromJson ( String pJson ) {

		Gson gson = new Gson();
		User vUser = gson.fromJson( pJson , User.class);

		  return vUser;
	}
	
	 public static Drawable resizeImage( Drawable drawable, int iconWidth, int iconHeight) {
		
		 Bitmap img = Bitmap.createBitmap(iconWidth, iconHeight, Bitmap.Config.ARGB_8888);
		 Canvas canvas = new Canvas(img);
		 //resize drawable here according to screen width
		// drawable.setBounds(-value * iconWidth, 0, -value * iconWidth + iconWidth*13, 50);
		 drawable.draw(canvas);
		    return drawable;

		  }
}
